package com.jinandi.app.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.jinandi.app.domain.Resource;
import com.jinandi.app.service.AuthoritAndResService;
import com.jinandi.app.service.AuthoritService;
import com.jinandi.app.service.ResourceService;

@Controller
@RequestMapping("/admin")
public class ResourceController extends BaseController {

	private ResourceService resourceService;

	private AuthoritService authoritService;
	
	private AuthoritAndResService authoritAndResService;
	
	@Autowired
	public void setAuthoritAndResService(AuthoritAndResService authoritAndResService) {
		this.authoritAndResService = authoritAndResService;
	}

	@Autowired
	public void setAuthoritService(AuthoritService authoritService) {
		this.authoritService = authoritService;
	}

	@Autowired
	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}

	@RequestMapping(value = "/res/modifyres.json", method = RequestMethod.POST)
	public void modifyres(WebRequest request,ModelMap modelMap){
		Map<String ,String> map = getStrictParams(request.getParameterMap());
		 try {
			 Resource resource = resourceService.findResById(Long.valueOf(map.get("id")));
			 resource.setUrl(map.get("url"));
			 resourceService.saveRes(resource);
			 modelMap.addAttribute("Result", "OK");
	     }catch (Exception e){
	    	 e.printStackTrace();
	    	 modelMap.addAttribute("Result", "ERROR");
	    	 modelMap.addAttribute("Message", e.getMessage());
	     }
	}
	
	@RequestMapping(value = "/res/createres.json", method = RequestMethod.POST)
	public void createres(WebRequest request,ModelMap modelMap){
		Map<String ,String> map = getStrictParams(request.getParameterMap());
		 try {
			 Resource resource = new Resource();
			 resource.setUrl(map.get("url"));
			 resourceService.saveRes(resource);
			 modelMap.addAttribute("Result", "OK");
			 modelMap.addAttribute("Record", resource);
	     }catch (Exception e){
	    	 e.printStackTrace();
	    	 modelMap.addAttribute("Result", "ERROR");
	    	 modelMap.addAttribute("Message", e.getMessage());
	     }
	}
	
	@RequestMapping(value = "/res/delres.json")
	public void delres(WebRequest request,ModelMap modelMap){
		Map<String ,String> map = getStrictParams(request.getParameterMap());
		 try {
			 resourceService.deleteResById(Long.valueOf(map.get("id")));
			 modelMap.addAttribute("Result", "OK");
	     }catch (Exception e){
	    	 e.printStackTrace();
	    	 modelMap.addAttribute("Result", "ERROR");
	    	 modelMap.addAttribute("Message", e.getMessage());
	     }
	}

	@RequestMapping(value = "/res/res.json", method = RequestMethod.POST)
	public void getAuthoritJson(WebRequest request, ModelMap modelMap) {
		Map<String, String> map = getStrictParams(request.getParameterMap());
		Map<String, Object> params = new HashMap<String, Object>();
		 if(map.containsKey("authoritId")){
			 params.put("authorit", authoritService.findAuthoritById(Long.valueOf(map.get("authoritId"))));
		 }
		Page<Resource> findRes = resourceService.findResLisr(params, map);
		getFormatJsonForJtable(modelMap, findRes);
	}

	/**
	 * <角色拥有的资源列表>
	 * @param id
	 * @param request
	 * @param modelMap
	 * @author CGC 
	 * 创建日期:2013-5-8 下午2:07:27
	 */
	@RequestMapping(value = "/res/{id}/res.json", method = RequestMethod.POST)
	public void getResByAuthorit(@PathVariable long id, WebRequest request,
			ModelMap modelMap) {
		List<Map<String, Object>> list = resourceService.findList(id);
		modelMap.addAttribute("Result", "OK");
		modelMap.addAttribute("TotalRecordCount", list.size());
		modelMap.addAttribute("Records", list);
	}
	
	@RequestMapping(value = { "/res/{aid}/modifyAuthoritAndRes.json" }, method = {RequestMethod.POST }, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String modifyAuthoritAndRes(@PathVariable Long aid ,WebRequest webRequest, Model model) {
		try {
			String[] ids = webRequest.getParameterValues("ids");
			authoritAndResService.modifyAuthoritAndRes(aid,ids);
			return "ok";
		} catch (Exception e) {
			e.printStackTrace();
			return "fail";
		}
	}

}
